package cn.iocoder.yudao.module.erp.service.warehouseinlist;

import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper;
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import cn.iocoder.yudao.module.erp.controller.admin.warehouseinlist.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.warehouseinlist.WarehouseInListDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.warehouseinlist.WarehouseInListMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * ERP 入库单表（一二级库） Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class WarehouseInListServiceImpl implements WarehouseInListService {

    @Resource
    private WarehouseInListMapper warehouseInListMapper;
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Long createWarehouseInList(WarehouseInListSaveReqVO createReqVO) {
        // 插入
        WarehouseInListDO warehouseInList = BeanUtils.toBean(createReqVO, WarehouseInListDO.class);
        warehouseInListMapper.insert(warehouseInList);
        // 返回
        return warehouseInList.getId();
    }

    @Override
    public void updateWarehouseInList(WarehouseInListSaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseInListExists(updateReqVO.getId());
        // 更新
        WarehouseInListDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseInListDO.class);
        warehouseInListMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouseInList(Long id) {
        // 校验存在
        validateWarehouseInListExists(id);
        // 删除
        warehouseInListMapper.deleteById(id);
    }

    private void validateWarehouseInListExists(Long id) {
        if (warehouseInListMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_IN_LIST_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseInListDO getWarehouseInList(Long id) {
        return warehouseInListMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseInListDO> getWarehouseInListPage(WarehouseInListPageReqVO pageReqVO) {
        PageResult<WarehouseInListDO> warehouseInListDOPageResult = warehouseInListMapper.selectPage(pageReqVO);
//        保留入库部门和出库部门不相等的并且入库部门为119
        warehouseInListDOPageResult.setList(warehouseInListDOPageResult.getList().stream()
                .filter(warehouseInListDO -> warehouseInListDO.getInDeptId().equals("119") && !warehouseInListDO.getInDeptId().equals(warehouseInListDO.getOutDeptId()))
                .collect(Collectors.toList()));
        if (warehouseInListDOPageResult.getList().isEmpty()){
            return PageResult.empty();
        }
//        创建者集合
        List<String> creatorList = warehouseInListDOPageResult.getList().stream().map(WarehouseInListDO::getCreator).collect(Collectors.toList());
        /*List<Long> inDeptIdList = warehouseInListDOPageResult.getList().stream().map(dept -> Long.parseLong(dept.getInDeptId())).collect(Collectors.toList());
        List<Long> outDeptIdList = warehouseInListDOPageResult.getList().stream().map(dept -> Long.parseLong(dept.getOutDeptId())).collect(Collectors.toList());*/
//        入库部门和出库部门集合
        List<Long> combinedDeptIdList = warehouseInListDOPageResult.getList().stream()
                .flatMap(dept -> Stream.of(dept.getInDeptId(), dept.getOutDeptId()))
                .filter(deptId -> deptId != null && !"null".equals(deptId))
                .map(Long::parseLong)
                .collect(Collectors.toList());
//        创建者集合对应的用户对象
        List<AdminUserDO> adminUserDOS = adminUserMapper.selectList(new QueryWrapperX<AdminUserDO>().in("id", creatorList));
//        部门对象
        List<DeptDO> deptDOList = deptMapper.selectList(new QueryWrapperX<DeptDO>().in("id", combinedDeptIdList));
        Map<String, String> creatorMap = adminUserDOS.stream().collect(Collectors.toMap(user -> String.valueOf(user.getId()), AdminUserDO::getNickname));
        Map<String, String> deptMap = deptDOList.stream().collect(Collectors.toMap(dept -> String.valueOf(dept.getId()), DeptDO::getName));
//        设置对应的值
        warehouseInListDOPageResult.getList().forEach(warehouseInListDO -> {
            warehouseInListDO.setCreator(creatorMap.get(warehouseInListDO.getCreator()));
            warehouseInListDO.setInDeptId(deptMap.get(warehouseInListDO.getInDeptId()));
            warehouseInListDO.setOutDeptId(deptMap.get(warehouseInListDO.getOutDeptId()));
        });
        return warehouseInListDOPageResult;
    }

}